﻿using System;
using imaginecup.information;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.componentInit;
using Imaginecup.util;
using Imaginecup.util.collision;
using Microsoft.Xna.Framework;
using Imaginecup.sprite.changeSourceSize;
using imaginecup.util;
using Imaginecup.util.resourceMan;
using Imaginecup.gameState.malaria.manager;
using Imaginecup.observer;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Input;

namespace Imaginecup.gameState.malaria.component.panel.clickAble
{
    public class CmpClickAblePanel : BaseSubjectEntity, ISourceInfoClass, IRelativeSourceInfoClass, IObserver, ISubject
    {
        private bool m_bPress;
     //   private ItemID m_ItemID;

        private ICmpItem m_Item;        

        EntityInfo m_SourceInfo = new EntityInfo();

        public CmpClickAblePanel(GObjectID a_GID, ObjectManager objMan , String textureName ,  ICmpItem item)
            : base(a_GID, objMan, ComponentTypeID.CID_ITEM_ENTITY)
        {
            m_bPress = false;

            m_SourceInfo.mSize = ResourceMan.GetResourceMan().GetFrameSize(textureName);

            m_Item = item;
          //  m_ItemID = itemID;
        }
                

        public override void Release()
        {
            base.Release();
        }

        public EntityInfo GetRelativeSource()
        {
            EntityInfo relativeInfo = m_SourceInfo;
#if WINDOWS
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

#endif

            if (m_Item.IsSelectAble() == false)
            {
                relativeInfo.mPosition = new Vector2(relativeInfo.mSize.X * 3, 0);
                relativeInfo.mSize.Y *= (float)m_Item.GetCoolTimeRate();
            }

            else if (m_bPress)
            {
                relativeInfo.mPosition = new Vector2(relativeInfo.mSize.X * 2, 0);
            }

#if WINDOWS
            else if(mouseInput.MouseState.LeftButton == ButtonState.Pressed &&
                Collision.CollisionByPoint(m_Info , mouseInput.Position) )
            {
                relativeInfo.mPosition = new Vector2(relativeInfo.mSize.X , 0);
            }
#endif
            else
            {
                relativeInfo.mPosition = Vector2.Zero;
            }

            return relativeInfo;
        }
        public Vector2 GetOffsetPos()
        {
            return Vector2.Zero;
        }
        public Vector2 GetOffsetSize()
        {
            Vector2 sizeOffset = Vector2.Zero;

            if (m_Item.IsSelectAble() == false)
            {
                Vector2 coolSize = m_Info.mSize;
                coolSize.Y *= (float)m_Item.GetCoolTimeRate();

                sizeOffset.Y = coolSize.Y - m_Info.mSize.Y;
            }

            return sizeOffset;
        }


        public EntityInfo GetSourceInfo()
        {
#if WINDOWS
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

#endif
            if( m_Item.IsSelectAble() == false )
            {
                m_SourceInfo.mPosition = new Vector2(m_SourceInfo.mSize.X * 3, 0);
            }
            else if( m_bPress )
            {
                m_SourceInfo.mPosition = new Vector2(m_SourceInfo.mSize.X * 2, 0);                
            }
#if WINDOWS
            else if (mouseInput.MouseState.LeftButton == ButtonState.Pressed && 
                Collision.CollisionByPoint(m_Info, mouseInput.Position))
            {
                m_SourceInfo.mPosition = new Vector2(m_SourceInfo.mSize.X, 0);
            }
#endif
            else
            {
                m_SourceInfo.mPosition = Vector2.Zero;
            }
            return m_SourceInfo;
        }

        public void Notify(Object data)
        {
            m_bPress = false;
            m_Item.DisableItem();
        }

        public override EMessageResult HandleMessage(GMessage a_Msg)
        {
            if (a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_CHECK_INPUT.MessageTypeID)
            {
              

//#if WINDOWS
                IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

                if (mouseInput.IsOneClickUp() && Collision.ContainPoint(m_Info.mPosition, m_Info.mSize, mouseInput.Position) && m_Item.IsSelectAble())
                {
                    m_Item.SelectItem();
                    NotifyAll( null ); // 다른 버튼들은 m_bPress = false;
                    m_bPress = true;
                }
                
                else if ( m_bPress)
                {
                    if( m_Item.IsUseAble() )
                    {
                        m_Item.UseItem((GameTime)a_Msg.m_Data);
                    }
                    
                    if( m_Item.IsStateEnd() )
                        m_bPress = false;
                }

//#endif
//#if WINDOWS_PHONE
//                IPhoneInputHandler input = PhoneInputHandler.GetInputHandler();
//                TouchCollection collection = input.GetCurrentTouch();
//                bool selectState = false;

//                foreach( TouchLocation touch in collection )
//                {
//                    if (touch.State == TouchLocationState.Pressed && Collision.ContainPoint(m_Info.mPosition, m_Info.mSize, touch.Position) && m_Item.IsSelectAble())
//                    {
//                         m_Item.SelectItem();
//                        NotifyAll( null ); // 다른 버튼들은 m_bPress = false;
//                        m_bPress = true;
//                        selectState = true;
//                    }
//                }

//                 if (selectState == false && m_bPress)
//                {
//                    if( m_Item.IsUseAble() )
//                    {
//                        m_Item.UseItem();
//                    }
                    
//                    if( m_Item.IsStateEnd() )
//                        m_bPress = false;
//                }

//#endif
               
            }

            return base.HandleMessage(a_Msg);
        }

       
    }
}
